let {codes} = require('keycode');

const step = 0.4;

// key listener
function onKeyDown(event) {
    let camera = inputMgr.camera;
    switch (event.keyCode) {
        case codes.s:
            camera.translateZ(-step);
            break;
        case codes.w:
            camera.translateZ(step);
            break;
        case codes.a:
            camera.translateX(step);
            break;
        case codes.d:
            camera.translateX(-step);
            break;
    }
}


function onKeyUp(event) {
}

//----------------------------------------
// mouse listener
function onMouseDown(event) {
    inputMgr.mouseDown = true;
    console.log("onMouseDown called!......");

    inputMgr.canvas.addEventListener('mousemove', onMouseMove, false);
    inputMgr.canvas.addEventListener('mouseup', onMouseUp, false);

    let [x, y] = inputMgr.getMousePos(event);
    inputMgr.startx = x;
    inputMgr.starty = y;
    inputMgr.lastx = x;
    inputMgr.lasty = y;
    // console.log(`x:${x},y:${y}`);
}

function onMouseMove(event) {
    console.log("onMouseMove called!......");
    let camera = inputMgr.camera;
    let [curX, curY] = inputMgr.getMousePos(event);
    let dx = curX - inputMgr.lastx;
    let dy = curY - inputMgr.lasty;
    inputMgr.lastx = curX;
    inputMgr.lasty = curY;
    if (Math.abs(dx) > Math.abs(dy)) {
        console.log("dx:",dx);
        // 水平滑动
        if (dx > 0) {
            // 从左往右
            camera.slide_left_to_right(-dx * 0.2);
        } else {
            // 从右往左
            camera.slide_left_to_right(-dx * 0.2);
        }
    } else {
        if (dy > 0) {
            //从上往下
            camera.slide_down_to_up(-dy * 0.2);
        } else {
            camera.slide_down_to_up(-dy * 0.2);
        }
    }
}


function onMouseUp(event) {
    inputMgr.mouseDown = false;
    console.log("onMouseUp called!......");
    inputMgr.canvas.removeEventListener('mousemove', onMouseMove);
    inputMgr.canvas.removeEventListener('mouseup', onMouseUp);
}

/**
 * 按下mouse (激活onMouseDown),
 * 然后将鼠标移动(激活onMouseMove)到canvas外面,
 * 最后松开 mouse, 不会激活 onMouseUp.
 * 我们让其激活  onMouseCancel
 * @param event
 */
function onMouseCancel(event) {
    console.log('onMouseCancel called!');
    onMouseUp(event);
}

// function onMouseEnter(event) {
// }

function onMouseLeave(event) {
    // console.log('onMouseLeave called!');
    if (inputMgr.mouseDown) {
        onMouseCancel(event);
    }
}

let inputMgr = {

    startx: -1,
    starty: -1,
    lastx: -1,
    lasty: -1,

    camera: null,
    canvas: null,
    mouseDown: false,
    init(canvas,camera) {
        console.log("注册按键......");
        this.canvas = canvas;
        this.camera = camera;

        // 注册事件
        // 添加按键事件
        document.addEventListener('keydown', onKeyDown, false);
        document.addEventListener('keyup', onKeyUp, false);

        // 添加mouse事件
        canvas.addEventListener('mousedown', onMouseDown, false);
        // canvas.addEventListener('mouseenter',onMouseEnter,false);
        canvas.addEventListener('mouseleave', onMouseLeave, false);
    },

    getMousePos(event) {
        let canvas = this.canvas;
        let rect = canvas.getBoundingClientRect();

        //获取鼠标在canvas上的位置
        let x = event.pageX - rect.left;
        let y = event.pageY - rect.top;
        return [x, y];
    }
};

module.exports = inputMgr;
